package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.Question;
import com.qfedu.entity.QuestionCountAndeType;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.User;
import com.qfedu.service.questionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.qfedu.entity.dataList;

@Controller      //我的资源模块
public class questionController {
    @Autowired
    private questionService questionService;



    //添加学习笔记
    @RequestMapping("/addNote")
    @ResponseBody
    public ResponseData addNote(String question,String answer, HttpServletRequest httpServletRequest){
        User user = null;
        HttpSession session = httpServletRequest.getSession();
        Object user1 = session.getAttribute("loginUser");
        if(user1!=null){
            user = (User)user1;
        }
        Question qs = new Question();
        qs.setFlag("笔记");
        qs.setQtype("笔记");
        qs.setuNo(user.getNo());
        qs.setAnswer(answer);
        qs.setQuestion(question);
        qs.setStar(0);
        ResponseData responseData = new ResponseData();
        int result = questionService.addNote(qs);
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg("添加成功！");
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("添加失败！");
        }
        return responseData;
    }



    //添加我的代码
    @RequestMapping("/addCode")
    @ResponseBody
    public ResponseData addCode(String question,String answer, HttpServletRequest httpServletRequest){
        User user = null;
        HttpSession session = httpServletRequest.getSession();
        Object user1 = session.getAttribute("loginUser");
        if(user1!=null){
            user = (User)user1;
        }
        Question qs = new Question();
        qs.setFlag("代码");
        qs.setQtype("代码");
        qs.setuNo(user.getNo());
        qs.setAnswer(answer);
        qs.setQuestion(question);
        qs.setStar(0);
        ResponseData responseData = new ResponseData();
        int result = questionService.addNote(qs);
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg("添加成功！");
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("添加失败！");
        }
        return responseData;
    }



    //新增资源
    @RequestMapping("/savequestion")
    @ResponseBody
    public ResponseData savequestion(int star,String type,String question,String answer, HttpServletRequest httpServletRequest){
        User user = null;
        HttpSession session = httpServletRequest.getSession();
        Object user1 = session.getAttribute("loginUser");
        if(user1!=null){
            user = (User)user1;
        }
        Question qs = new Question();
        qs.setFlag("资源");
        qs.setQtype(type);
        qs.setuNo(user.getNo());
        qs.setAnswer(answer);
        qs.setQuestion(question);
        qs.setStar(star);
        ResponseData responseData = new ResponseData();
        int result = questionService.addNote(qs);
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg("添加成功！");
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("添加失败！");
        }
        return responseData;
    }


    //资源库分页查询
    @RequestMapping("/queryquestionbyno")
    @ResponseBody
    public ResponseData<List<Question>> queryquestionbyno(int page, int limit){
        PageHelper.startPage(page,limit);
        ResponseData<List<Question>> responseData = new ResponseData();
        List<Question> questionList = questionService.getAllQuestion();
        responseData.setCode(0);
        PageInfo pageInfo = new PageInfo(questionList);
        responseData.setCount((int)pageInfo.getTotal());
        responseData.setData(questionList);
        return responseData;
    }



    //更新资源
    @RequestMapping("/updateQuestion")
    @ResponseBody
    public ResponseData updateQuestion(int id,String type,String question,String answer){
        Question questionstr = questionService.getQuestionById(id);
        questionstr.setAnswer(answer);
        questionstr.setQtype(type);
        questionstr.setQuestion(question);
        int result = questionService.updateQuestion(questionstr);
        ResponseData responseData = new ResponseData();
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg("修改成功！");
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("修改失败！");
        }
        return responseData;
    }


    //删除资源
    @RequestMapping("/deleteQuestion")
    @ResponseBody
    public ResponseData questiondelete(int id){
        int result = questionService.deleteQuestionById(id);
        ResponseData responseData = new ResponseData();
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg("删除成功！");
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("删除失败！");
        }
        return responseData;
    }


    //动态显示资源eCharts图
    @RequestMapping("/eCharts")
    @ResponseBody
    public dataList eCharts(){
        List<QuestionCountAndeType> questionCountAndeTypeList = questionService.getAllTypeCountAndType();
        List<String> list = new ArrayList();
        List<Integer> list1 = new ArrayList();
        for(QuestionCountAndeType questionCountAndeType:questionCountAndeTypeList){
            list.add(questionCountAndeType.getQtype());
            list1.add(questionCountAndeType.getCount());
        }
        dataList dataList = new dataList();
        dataList.setStringList(list);
        dataList.setIntegerList(list1);
        return dataList;
    }
}
